package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.entity.ExploreTemplate;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.service.IExploreTemplateService;
import com.bridgeintelligent.tag.utils.PasswordHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Api(tags = "画像分析管理")
@AllArgsConstructor
@RestController
@RequestMapping("/api/explore")
@Slf4j
public class ExploreTemplateController {

    private IExploreTemplateService templateService;

    @ApiOperation("新增画像分析")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "template", value = "模板信息")
    })
    @PostMapping("/add")
    public Result addExploreTemplate(@RequestBody ExploreTemplate template) {
        try {
            //校验特殊字符
            if ( PasswordHelper.checkSpecial(template.getAlName())){
                return new Result(-1,"名字含有特殊字符！");
            }
            //校验分析名是否重复
            boolean success = templateService.checkAlName(template.getId(),template.getAlName());
            if (success){
                return new Result(-1, "分析名字重复，请修改分析名字后重新提交！");
            }
            templateService.addExploreTemplate(template);
            return new Result(0,"保存成功");
        } catch (Exception e) {
            log.error("新增画像分析失败：{}::"+e);
            return new Result(-1,"新增失败");
        }
    }


    @ApiOperation("修改画像分析")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", name = "template", value = "分析信息")
    })
    @PostMapping("/update")
    public Result updateExploreTemplate(@RequestBody ExploreTemplate template) {
        try {
            //校验特殊字符
            if ( PasswordHelper.checkSpecial(template.getAlName())){
                return new Result(-1,"名字含有特殊字符！");
            }
            //校验分析名是否重复
            boolean success = templateService.checkAlName(template.getId(),template.getAlName());
            if (success) {
                return new Result(-1, "分析名字重复，请修改分析名字后重新提交！！");
            }
            templateService.updateExploreTemplate(template);
            return new Result(0,"修改成功");
        } catch (Exception e) {
            log.error("修改画像分析失败：{}::"+e);
            return new Result(-1, "修改失败");
        }
    }

    @ApiOperation("删除画像分析")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "探查ID")})
    @GetMapping("/del")
    public Result delExploreTemplate(String id) {
        try {
            templateService.delExploreTemplate(id);
            return new Result(0,"删除成功");
        }catch (Exception e){
            log.error("删除画像分析失败：{}::"+e);
            return new Result(-1,"删除失败");
        }

    }

    @ApiOperation("画像分析详情")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "探查ID")
    })
    @GetMapping("/findOne")
    public Result findOne(String id) {
        try {
            ExploreTemplate template = templateService.findExploreTemplate(id);
            return new Result(0,"查询成功",template);
        }catch (Exception e){
            log.error("查看画像分析详情失败：{}::"+e);
            return new Result(-1,"查看画像详情失败"+e.getMessage());
        }
    }

    @ApiOperation("分页画像分析列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "页码",required = false),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数" , required = false),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间" ,required = false),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间",required = false),
            @ApiImplicitParam(paramType = "query", name = "alName", value = "宏观画像名称" ,required = false)
    })
    @GetMapping("/pageList")
    public Result pageList(Integer pageNumber, Integer pageSize, String startTime,String endTime,String alName) {

        try {
            return templateService.pageList(pageNumber,pageSize,startTime,endTime,alName);
        }catch (Exception e){
            log.error("分页查询失败：{}::"+e);
            return new Result(-1,"分页查询失败");
        }
    }
}